package com.mygdx.game.newmap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;

/**
 * Created by dell on 2018/2/6.
 * 走动的人
 */
public class Person extends Actor {

    private Texture texture;

    private Animation<TextureRegion> rightAnim; //向右走动画
    private Animation<TextureRegion> leftAnim;
    private Animation<TextureRegion> topAnim;
    private Animation<TextureRegion> botAnim;

    private float x;
    private float y;
    private float stateTime; //当前时长


    enum STATE{
        LEFT,  //向左走
        RIGHT, //向右走
        TOP,   //向上
        BOTTOM, //向下
        IDLE   //静止
    }
    private STATE state;
    public Person(float x, float y) {
        this.x = x;
        this.y = y;
        setX(x);
        setY(y);
        this.stateTime = 0;
        state = STATE.IDLE;
        show();
    }

    public STATE getState() {
        return state;
    }

    public void setState(STATE state) {
        this.state = state;
        preState = state;
    }
    public void updateState(STATE state){
        this.state = state;
    }

    private void show() {
        texture = new Texture(Gdx.files.internal("data/map2/person2.png"));
        int rows = 4;
        int cols = 4;
        int frameW = texture.getWidth() / cols;
        int frameH = texture.getHeight() / rows;
        TextureRegion[][] split = TextureRegion.split(texture, frameW, frameH);

        //向右走
        TextureRegion[] rightRegion = new TextureRegion[cols];
        int index = 0;
        for (int col = 0; col < cols; col++) {
            rightRegion[index++] = split[2][col];  //拿到第三行水平行走的帧
        }
        rightAnim = new Animation<TextureRegion>(0.1f,rightRegion);
        rightAnim.setPlayMode(Animation.PlayMode.NORMAL);
        //向左走
        TextureRegion[] leftRegion = new TextureRegion[cols];
        index = 0;
        for (int col = 0; col < cols; col++) {
            leftRegion[index++] = split[1][col];  //拿到第三行水平行走的帧
        }
        leftAnim = new Animation<TextureRegion>(0.1f,leftRegion);
        leftAnim.setPlayMode(Animation.PlayMode.LOOP);

        //向上
        TextureRegion[] topRegion = new TextureRegion[cols];
        index = 0;
        for (int col = 0; col < cols; col++) {
            topRegion[index++] = split[3][col];  //拿到第三行水平行走的帧
        }

        topAnim = new Animation<TextureRegion>(0.1F,topRegion);
        topAnim.setPlayMode(Animation.PlayMode.LOOP);

        //向下
        TextureRegion[] bottomRegion = new TextureRegion[cols];
        index = 0;
        for (int col = 0; col < cols; col++) {
            bottomRegion[index++] = split[0][col];  //拿到第三行水平行走的帧
        }
        botAnim = new Animation<TextureRegion>(0.1F,bottomRegion);
        botAnim.setPlayMode(Animation.PlayMode.LOOP);

    }


    @Override
    public void act(float delta) {
        super.act(delta);
    }

    private TextureRegion curRegion;
    @Override
    public void draw(Batch batch, float parentAlpha) {
        stateTime +=Gdx.graphics.getDeltaTime();
        checkStatus();
        batch.draw(curRegion,getX(),getY());
    }

    private STATE preState; //上一个状态
    private void checkStatus() {
        if (state== STATE.RIGHT) {
            preState = state;
            curRegion = rightAnim.getKeyFrame(stateTime,true);
        }else if (state == STATE.LEFT){
            preState = state;
            curRegion = leftAnim.getKeyFrame(stateTime,true);
        }else if (state==STATE.TOP){
            preState = state;
            curRegion = topAnim.getKeyFrame(stateTime,true);
        }else if (state==STATE.BOTTOM){
            preState = state;
            curRegion = botAnim.getKeyFrame(stateTime,true);
        }else if (state == STATE.IDLE)
        {
            if (preState==STATE.RIGHT)
            {
                curRegion = rightAnim.getKeyFrames()[0];
            }else if (preState==STATE.LEFT)
            {
                curRegion = leftAnim.getKeyFrames()[0];
            }else if (preState==STATE.TOP){
                curRegion = topAnim.getKeyFrames()[0];
            }else if (preState==STATE.BOTTOM){
                curRegion = botAnim.getKeyFrames()[0];
            }else {   //默认初始的方向
                curRegion = rightAnim.getKeyFrames()[0];
            }
        }
    }
}
